<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>新增请假</title>
    <link rel="stylesheet" href="../../../component/pear/css/pear.css"/>
</head>
<body>
<form class="layui-form">
    <div class="mainBox">
        <div class="main-container">
            <div class="main- container">

                <div class="layui-form-item">
                    <label class="layui-form-label">开始时间</label>
                    <div class="layui-input-block">
                        <select lay-verify="" name="startTime_former">
                            <option value="0">今天</option>
                            <option value="1">明天</option>
                            <option value="2">后天</option>
                        </select>
                        <select id="startTime" lay-verify="" lay-verify="required"
                                name="startTime" lay-reqText="请选择请假开始时间">
                        </select>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">结束时间</label>
                    <div class="layui-input-block">
                        <select lay-verify="" lay-filter="endTime" name="endTime_former">
                            <option value="0">当天</option>
                            <option value="1">第2天</option>
                            <option value="2">第3天</option>
                        </select>
                        <select id="endTime" lay-verify="" name="endTime" lay-verify="required"
                                name="endTime" lay-reqText="请选择请假结束时间">
                        </select>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">请假原因</label>
                    <div class="layui-input-block">
                        <textarea id="leaveReason" name="leaveReason" required lay-verify="title"
                                  placeholder="请输入你的请假原因"
                                  lay-verify="required" lay-reqText="请输入你的请假原因"
                                  class="layui-textarea" style="height: 210px"></textarea>
                    </div>
                </div>

            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="layui-btn layui-btn-sm" lay-submit="" lay-filter="user-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交申请
            </button>
        </div>
    </div>
</form>
<script src="../../../component/layui/layui.js"></script>
<script src="../../../component/pear/pear.js"></script>
<script>
    layui.use(['form', 'jquery', 'loading', 'laydate'], function () {
        let form = layui.form;
        let $ = layui.jquery;
        let loading = layui.loading;
        let laydate = layui.laydate;
        let my_date = new Date();
        let my_year = my_date.getFullYear();
        let my_month = my_date.getMonth() + 1;
        let my_day = my_date.getDate();
        let startTime = my_year + '-' + my_month + '-' + my_day;
        let endTime = my_year + '-' + my_month + '-' + (my_day + 1);
        let startTime_select = $('#startTime');
        let endTime_select = $('#endTime');

        function renderTime() {
            for (let i = 6; i < 24; i++) {
                let j = i;
                if (i < 10) {
                    j = '0' + i + ':00';
                } else {
                    j = i + ':00';
                }
                //console.log(j);
                endTime_select.append(' <option value="' + j + '">' + j + '</option>');
                startTime_select.append(' <option value="' + j + '">' + j + '</option>');
            }
            form.render('select')
        };
        renderTime();
        form.on('submit(user-save)', function (data) {
            if (data.field.startTime_former === '0') {
                my_day = my_date.getDate();
                startTime = my_year + '-' + my_month + '-' + my_day;
            } else if (data.field.startTime_former === '1') {
                my_day = my_date.getDate();
                my_day++;
                startTime = my_year + '-' + my_month + '-' + my_day;
            } else {
                my_day = my_date.getDate();
                my_day += 2;
                startTime = my_year + '-' + my_month + '-' + my_day;
            }
            // --------分隔线---------
            if (data.field.endTime_former === '0') {
                endTime = my_year + '-' + my_month + '-' + my_day;
            } else if (data.field.endTime_former === '1') {
                my_day++;
                endTime = my_year + '-' + my_month + '-' + my_day;
            } else {
                my_day += 2;
                endTime = my_year + '-' + my_month + '-' + my_day;
            }
            console.log(startTime + ' ' + data.field.startTime);
            console.log(endTime + ' ' + data.field.endTime);
            loading.Load(3, "");
            $.ajax({
                url: '/StudentLeave/insertInfo',
                data: {
                    startTime: startTime + ' ' + data.field.startTime + ':00',
                    endTime: endTime + ' ' + data.field.endTime + ':00',
                    leaveReason: data.field.leaveReason
                },
                dataType: 'json',
                type: 'post',
                success: function (result) {
                    loading.loadRemove();
                    console.log(result);
                    if (result.success == '1') {
                        layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            //console.log(JSON.stringify(result.data));
                            parent.layui.table.reload("user-table", {
                                data: result.data,
                                page: {
                                    layout: ['count', 'prev', 'page', 'next', 'skip']
                                    , curr: parent.layui.jquery(".layui-laypage-em").next().html()
                                    , groups: 3
                                    , last: false
                                }
                            });
                        });
                    } else {
                        layer.open({
                            title: '提示'
                            , icon: 2
                            , content: result.msg
                        });
                    }
                }
            })
            return false;
        });
    })

</script>
</body>
</html>